//
//  MDYRequest.m
//  TestGizWits
//
//  Created by JM on 15/11/17.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "MDYRequest.h"
#import <XPGWifiSDK/XPGWifiSDK.h>
#import "DYServer.h"
#import <objc/runtime.h>
@interface MDYRequest()
@end

@implementation MDYRequest

+ (instancetype) request {
    return [[[self class] alloc] init];
}

- (void)requstWithCompletionHandler:(FinishLoadBlock)aBlock;{
    unsigned int outCount, i;
    objc_property_t * properties = class_copyPropertyList([self class], &outCount);
    
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        if (propertyName && [[propertyName substringToIndex:3] isEqualToString:@"DY_"]) {
            id propertyValue = [self valueForKey:(NSString *)propertyName];
            NSAssert(propertyValue, @"%@请求参数%@不能为nil",[self class],propertyName);
        }
    }
    free(properties);
    
    self.block = aBlock;
    [[DYServer sharedDYServer] addRequest:self];
}

- (void)didRealRequest {
    NSAssert(0, @"必须实现");
}

- (void)superHandelResponse {
    if ([DYServer sharedDYServer].receiveDelegate) {
            [XPGWifiSDK sharedInstance].delegate = [DYServer sharedDYServer].receiveDelegate;
    } else {
        [XPGWifiSDK sharedInstance].delegate = [DYServer sharedDYServer];
    }
     [[DYServer sharedDYServer] removeRequest:self];
}

- (void)print {
    //对真实类的成员进行打印。todo
    NSLog(@"DYResponse:%@",[self description]);
}

- (NSString *)description {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t * properties = class_copyPropertyList([self class], &outCount);
    
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        if (propertyName && [[propertyName substringToIndex:3] isEqualToString:@"DY_"]) {
            id propertyValue = [self valueForKey:(NSString *)propertyName];
            if (!propertyValue){
                propertyValue = @"null";
            }
            [dic setObject:propertyValue forKey:propertyName];
            
        }
    }
    free(properties);
    
    return [NSString stringWithFormat:@"发送的request: %@",dic];//对真实类的成员进行打印。todo
}
@end
